package com.machine.action.order;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.bean.PageBean;
import com.machine.bean.PriceOrder;
import com.machine.service.order.PriceOrderServer;
import com.opensymphony.xwork2.ActionSupport;

@Component("findPriceOrderAction")
@Scope(value = "prototype")
public class FindPriceOrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PriceOrderServer priceOrderServer;
	private List<PriceOrder> priceOrders = null;
	private String providerName;
	private String buyName;
	
	private int page; // 第几页

	private PageBean pageBean; // 包含分布信息的bean

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}


	public PriceOrderServer getPriceOrderServer() {
		return priceOrderServer;
	}

	@Resource(name = "priceOrderServer")
	public void setPriceOrderServer(PriceOrderServer priceOrderServer) {
		this.priceOrderServer = priceOrderServer;
	}

	public List<PriceOrder> getPriceOrders() {
		return priceOrders;
	}

	public void setPriceOrders(List<PriceOrder> priceOrders) {
		this.priceOrders = priceOrders;
	}

	public String getProviderName() {
		return providerName;
	}

	public void setProviderName(String providerName) {
		this.providerName = providerName;
	}

	public String getBuyName() {
		return buyName;
	}

	public void setBuyName(String buyName) {
		this.buyName = buyName;
	}

	@Override
	public String execute() throws Exception {
		
		if (page <= 0) {
			page = 1;
		}

		pageBean = priceOrderServer.findAll(providerName,buyName,page,3);
		priceOrders = pageBean.getList();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out
				.print("<form method='post' class='orderform' ><table class='back_table' cellspacing='0' cellpadding='0'>");
		out
				.print("<tr><th width='30'>选择</th><th>编号</th><th>供应商编号</th><th>供应商名称</th><th>采购商编号</th><th>采购商名称</th><th>零部件编号</th><th>零部件名称</th><th>零部件原价</th><th>零部件供应商出价</th><th>采购商出价</th><th>说明信息</th></tr>");

		if (priceOrders != null) {
			for (int i = 0; i < priceOrders.size(); i++) {
				out.print("<tr>");
				out.print("<th width='20'><input name='id' value='"
						+ priceOrders.get(i).getId()
						+ "' type='checkbox'></th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getId());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getProviderid());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getProviderName());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getBuyid());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getBuyName());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getComponentid());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getComponentName());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getPrice());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getProviderprice());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getBuyprice());
				out.print("</th>");
				out.print("<th>");
				out.print(priceOrders.get(i).getInfo());
				out.print("</th>");
				out.print("</tr>");
			}
			
			out.print("<tr>");
			out.print("<td>");
			if(pageBean.getCurrentPage() == 1){
				out.print("上一页	首页");
			}else{
				out.print("<a onclick='return changeframe("+(pageBean.getCurrentPage() - 1)+");' >上一页</a>");
				out.print("<a onclick='return changeframe(1);'>首页</a>");
			}

			
			out.print("</td>");
			out.print("<td>");
			if(pageBean.getCurrentPage() != pageBean.getTotalPage()){
			
			out.print("<a onclick='return changeframe("+(pageBean.getCurrentPage() + 1)+");' >下一页</a>");

			out.print("<a onclick='return changeframe("+pageBean.getTotalPage()+");'>尾页</a>");
			}else{
				out.print("下一页	尾页");
			}

			out.print("共" + pageBean.getAllRow() + "条记录");
			out.print("</td>");
			out.print("</tr>");

		}
		out.print("</table>");
		out.print("</form>");
		out.flush();
		out.close();

		return super.execute();
	}

}
